﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace SharingCenter.Web.Classes
{
    public class NickNameLink : HyperLink
    {
        public Guid UserId { get; set; }
        public Guid CurrentUserId { get; set; }
        public bool ShowOnlineStatus { get; set; }
        public string OnlineImageUrl { get; set; }
        public string OnlineText { get; set; }

        public NickNameLink()
        {
            this.PreRender += new EventHandler(NickNameLink_PreRender);
        }

        void NickNameLink_PreRender(object sender, EventArgs e)
        {
            RenderNickName();
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            RenderNickName();
            base.Render(writer);
        }

        void RenderNickName()
        {
            if (CurrentUserId == UserId)
            {
                Text = "我";
            }
            else
            {
                try
                {
                    var q = from t in scDataContext.Instance.Sc_Profiles
                            join m in scDataContext.Instance.aspnet_Users
                            on t.UserId equals m.UserId
                            where t.UserId == UserId
                            select new
                            {
                                t.NickName,
                                m.UserName
                            };
                    var h = q.First();
                    Text = h.NickName;
                    NavigateUrl = string.Format("~/Home/?id={0}", UserId);
                    if (ShowOnlineStatus)
                    {
                        MembershipUser u = Membership.GetUser(h.UserName);
                        if (u.IsOnline)
                        {
                            Image onlineImage = new Image();
                            onlineImage.ImageUrl = OnlineImageUrl;
                            onlineImage.AlternateText = OnlineText;
                            onlineImage.ToolTip = OnlineText;
                            Controls.Add(onlineImage);
                            Label l = new Label();
                            l.Text = h.NickName;
                            Controls.Add(l);
                        }
                    }
                }
                catch { }
            }
            

            
        }
    }
}
